package com.dacong.nezha.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dacong.nezha.domain.Active;
import com.dacong.nezha.domain.BindAccount;
import com.dacong.nezha.mapper.BindAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BindAccountService extends ServiceImpl<BindAccountMapper,BindAccount> {

    @Autowired
    BindAccountMapper bindAccountMapper;

    @Autowired
    ActiveService activeService;

    public Boolean bind(String code,String account){

        Active one = activeService.lambdaQuery()
                .eq(Active::getCode, code)
                .one();
        if (one != null) {

            BindAccount isBind = lambdaQuery().eq(BindAccount::getCode, code)
                    .eq(BindAccount::getAccount, account)
                    .one();
            if (isBind != null){
                return true;
            }
            Integer authNum = one.getAuthNum();
            Long count = lambdaQuery().eq(BindAccount::getCode, code)
                    .count();
            if (count < authNum){
                BindAccount bindAccount = new BindAccount();
                bindAccount.setCode(code);
                bindAccount.setAccount(account);
                save(bindAccount);
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
}
